﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.SalesAndCustomerRelations
{
    public class CustomerInfos : DetailView<customerinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<customerinfo> EntityList;
        private new customerinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<customerinfo>

        public override List<customerinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.customerinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<customerinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.customerinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override customerinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new customerinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTocustomerinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<customerinfo> GetEntityListByParameters(long customerTypeId, DateTime? contractEndDate, string companyName, string companyNoText, long serviceTypeId, string securitySupervisor, long capitalStructureId)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (customerTypeId > 0)
            {
                dynamicString += " And it.c_CustomerTypeParameterValueID_20 = @CustomerTypeId";
                parameterList.Add(new ObjectParameter("CustomerTypeId", customerTypeId));
            }

            if (contractEndDate != null)
            {
                dynamicString += " And it.m_ContractEndDate = @ContractEndDate";
                parameterList.Add(new ObjectParameter("ContractEndDate", contractEndDate));
            }

            if (!string.IsNullOrEmpty(companyName) && !string.IsNullOrWhiteSpace(companyName))
            {
                dynamicString += " And it.b_CompanyName = @CompanyName";
                parameterList.Add(new ObjectParameter("CompanyName", companyName));
            }

            if (!String.IsNullOrEmpty(companyNoText) && !String.IsNullOrWhiteSpace(companyNoText))
            {
                dynamicString += " And it.d_CompanyNo = @CompanyNo";
                parameterList.Add(new ObjectParameter("CompanyNo", Convert.ToInt32(companyNoText)));
            }

            if (serviceTypeId > 0)
            {
                dynamicString += " And it.o_ServiceTypeParameterValueID_23 = @ServiceTypeId";
                parameterList.Add(new ObjectParameter("ServiceTypeId", serviceTypeId));
            }

            if (!string.IsNullOrEmpty(securitySupervisor) && !string.IsNullOrWhiteSpace(securitySupervisor))
            {
                dynamicString += " And it.j_SecuritySupervisor = @SecuritySupervisor";
                parameterList.Add(new ObjectParameter("SecuritySupervisor", securitySupervisor));
            }

            if (capitalStructureId > 0)
            {
                dynamicString += " And it.i_CapitalStructureParameterValueID_22 = @CapitalStructureId";
                parameterList.Add(new ObjectParameter("CapitalStructureId", capitalStructureId));
            }

            EntityList = _context.customerinfoes.Where(dynamicString, parameterList.ToArray()).
                OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public List<GetCustomerTypeInfoReport_Result> GetCustomerTypeInfoReport()
        {
            _context = new project5188Entities();

            return _context.ExecuteFunction<GetCustomerTypeInfoReport_Result>("GetCustomerTypeInfoReport").ToList();
        }

        public List<GetMonthlyCustomerCountReport_Result> GetMonthlyCustomerCountReport()
        {
            var list = new List<GetMonthlyCustomerCountReport_Result>();

            _context = new project5188Entities();

            for (var i = 1; i <= 12; i++)
            {
                var objectParameters = new ObjectParameter[1];

                var currentMonthParameter = new ObjectParameter("CurrentMonth", typeof(int)) { Value = i };
                objectParameters[0] = currentMonthParameter;

                var queryResult = _context.ExecuteFunction<GetMonthlyCustomerCountReport_Result>("GetMonthlyCustomerCountReport", objectParameters);

                list.AddRange(queryResult.Select(getMonthlyCustomerCountReportResult => new GetMonthlyCustomerCountReport_Result
                                                                                            {
                                                                                                Counter = getMonthlyCustomerCountReportResult.Counter, MonthCounter = getMonthlyCustomerCountReportResult.MonthCounter
                                                                                            }));
            }

            return list;
        }

        public List<GetServiceTypeInfoReport_Result> GetServiceTypeInfoReport()
        {
            _context = new project5188Entities();

            return _context.ExecuteFunction<GetServiceTypeInfoReport_Result>("GetServiceTypeInfoReport").ToList();
        }
    }
}
